<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="'add'" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { getListByPage, editSave ,getTjdetail} from '@/api/claim/factory/FaultAppearMaintain';
export default {
  props: ['type', 'rowData'],
  components: {
  },
  data() {
    return {
      loading: false,
      visible: false,
      visibleDrawer: false,
      visibleAdd: false,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      formList: this.createFormList(),
      saveData: null,
      val: null,
      level: 4
    };
  },
  created() {
  },
  mounted() {
    this.getDetailFun(this.rowData.faultId);
  },
  methods: {
    //一级下拉框
    async getLevelOne() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getListByPage({
        level: 1,
        pageNum: 1,
        pageSize: 99999,
        faStatus: 1
      });
      console.log("==========",res.data);
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'sys1FaultId').initialValue = '';
        this.formList.find(x => x.fieldName == 'sys1FaultId').itemList = res.data.records.map(y => ({ text: y.sysName1, value: y.faultId }));
      }
    },
    //二级下拉框
    async getLevelTwoByParentFaultId(parentFaultId) {
      console.log("===== 获取下拉框数据 ====== ")
      this.formList.find(x => x.fieldName == 'sys2FaultId').initialValue = '';
      const res = await getListByPage({
        level: 2,
        pageNum: 1,
        pageSize: 99999,
        parentFaultId: parentFaultId,
        faStatus: 1
      });
      console.log("==========",res.data);
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'sys2FaultId').initialValue = '';
        this.formList.find(x => x.fieldName == 'sys2FaultId').itemList = res.data.records.map(y => ({ text: y.sysName2, value: y.faultId }));
      }
    },
    //三级下拉框
    async getLevelThreeByParentFaultId(parentFaultId) {
      console.log("===== 获取下拉框数据 ====== ")
      this.formList.find(x => x.fieldName == 'parentFaultId').initialValue = '';
      const res = await getListByPage({
        level: 3,
        pageNum: 1,
        pageSize: 99999,
        parentFaultId: parentFaultId,
        faStatus: 1
      });
      console.log("==========",res.data);
      if (res.resultCode == 200) {
        this.formList.find(x => x.fieldName == 'parentFaultId').initialValue = '';
        this.formList.find(x => x.fieldName == 'parentFaultId').itemList = res.data.records.map(y => ({ text: y.sysName3, value: y.faultId }));
      }
    },
    async getDetailFun(id) {
      await this.getLevelOne();
      this.loading = true;
      let res = (await getTjdetail({ id: id, level: this.level })).data;
      await this.getLevelTwoByParentFaultId(res.sys1FaultId);
      await this.getLevelThreeByParentFaultId(res.sys2FaultId);
      for (let i in res) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res[i];
          }
        });
      }
      this.modelName = res.modelName;
      this.ptStatus = res.ptStatus;
      this.ownerCode = res.ownerCode;
      this.appId = res.appId;
      this.ownerParCode = res.ownerParCode;
      this.createdBy = res.createdBy;
      console.log(res, 'res');
      this.loading = false;
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    // async saveDialog() {
    //   this.$refs.formPanel.SUBMIT_FORM();
    //   if (this.saveData !== null) {
    //     console.log(this.saveData);
    //     this.saveData.ownerCode = this.ownerCode;
    //     this.saveData.ownerParCode = this.ownerParCode;
    //     this.saveData.appId = this.appId;
    //     this.saveData.faultId=this.rowData.faultId;
    //     this.saveData.isDeleted=this.rowData.isDeleted;
    //     this.saveData.createdBy=this.createdBy;
    //     this.saveData.createdAt=this.rowData.createdAt;
    //     this.saveData.level = this.level;
    //     this.saveData.isDeleted=this.rowData.isDeleted;
    //     console.log(this.saveData);
    //   }
    // },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        console.log(this.saveData);
        this.saveData.ownerCode = this.ownerCode;
        this.saveData.ownerParCode = this.ownerParCode;
        this.saveData.appId = this.appId;
        this.saveData.faultId=this.rowData.faultId;
        this.saveData.isDeleted=this.rowData.isDeleted;
        this.saveData.createdBy=this.createdBy;
        this.saveData.createdAt=this.rowData.createdAt;
        this.saveData.level = this.level;
        // this.saveData.isDeleted=this.rowData.isDeleted;
        this.saveData.isDeleted=0;
        this.loading = true;
        let res = await editSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
        }
      }
    },
    //保存
    chooseOrderNo() {
      this.visibleDrawer = true;
    },
    closeAdd() {
      this.visibleDrawer = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleDrawer = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '一级系统名称',
          fieldName: 'sys1FaultId',
          placeholder: '请选择一级系统名称...',
          initialValue: '',
          itemList: [],
          rules: [{required: true, message: '请选择一级系统名称...', trigger: 'change'}],
          change: val => {
            console.log("选择一级系统名称", val)
            this.getLevelTwoByParentFaultId(val)
          },
          disabled: true,
          readonly: true
        },
        {
          type: 'SELECT',
          label: '二级系统名称',
          fieldName: 'sys2FaultId',
          placeholder: '请选择一级系统名称...',
          initialValue: '',
          itemList: [],
          rules: [{required: true, message: '请选择二级系统名称...', trigger: 'change'}],
          change: val => {
            console.log("选择二级系统名称")
            this.getLevelThreeByParentFaultId(val)
          },
          disabled: true,
          readonly: true
        },
        {
          type: 'SELECT',
          label: '三级系统名称',
          fieldName: 'parentFaultId',
          placeholder: '请选择三级系统名称...',
          initialValue: '',
          itemList: [],
          rules: [{required: true, message: '请选择三级系统名称...', trigger: 'change'}],
          change: val => {
            console.log("选择三级系统名称")
          },
          disabled: true,
          readonly: true
        },
        {
          type: 'INPUT',
          label: '故障现象代码',
          fieldName: 'sysCode4',
          placeholder: '请输入故障现象代码...',
          initialValue: '',
          disabled: true,
          readonly: true
        },
        {
          type: 'INPUT',
          label: '故障现象名称',
          fieldName: 'sysName4',
          placeholder: '请输入故障现象名称...',
          maxlength: 50,
          initialValue: '',
          rules: [{required: true, message: '请输入故障现象名称...', trigger: 'change'}]
        },
        // {
        //   type: 'INPUT',
        //   label: '三级',
        //   fieldName: 'sysCode3',
        //   placeholder: '请输入三级...',
        //   initialValue: '',
        //   rules: [{required: true, message: '请输入三级...', trigger: 'change'}]
        // },
        // {
        //   type: 'INPUT',
        //   label: '三级名称',
        //   fieldName: 'sysName3',
        //   placeholder: '请输入三级名称...',
        //   initialValue: '',
        //   rules: [{required: true, message: '请输入三级名称...', trigger: 'change'}]
        // },
        // {
        //   type: 'INPUT',
        //   label: '故障代码',
        //   fieldName: 'sysCode4',
        //   placeholder: '请输入故障代码...',
        //   initialValue: '',
        //   rules: [{required: true, message: '请输入故障代码...', trigger: 'change'}]
        // },
        // {
        //   type: 'INPUT',
        //   label: '故障名称',
        //   fieldName: 'sysName4',
        //   placeholder: '请输入故障名称...',
        //   initialValue: '',
        //   rules: [{required: true, message: '请输入故障名称...', trigger: 'change'}]
        // },
        {
          type: 'SELECT',
          label: '是否有效',
          placeholder: '请选择是否有效...',
          filterable: true,
          fieldName: 'faStatus',
          itemList: [{text: '否', value: 0}, {text: '是', value: 1}],
          rules: [{required: true, message: '请选择是否有效...', trigger: 'change'}]
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
